$(function () {
    let markid = getUrlString('markid');
    let xiandanid = getUrlString('id');
    let showrmb = 2,moneyrmb,kutoysid,show = 0,exgprice,cost,bukuan_status,dingjin_money2;
    function detail() {
        let data = {
            'markid':markid,
            'kuid':xiandanid
        };
        getData('post','/niu/apiv3/niu_getkuinfo.php',data,function (data) {
            console.log(data);
            exgprice = Number(data.toys.exgprice);
            cost = Number(data.toys.cost);
            bukuan_status = data.bukuan_status;
            dingjin_money2 = Number(data.dingjin_money2);
            $('.toyspic').attr('src',data.toys.pics);
            $('.toysname').html(data.toys.name);
            $('#priceGold').val(data.money2);
            $('#intro').val(data.intro);
            moneyrmb = Math.ceil(data.money2/6)*100;
            let moneyRMB = Math.ceil(data.money2 / 6);
            $('#showrmbNum').html(moneyRMB);
            kutoysid  = data.kutoysid;
            if (data.ispw === "1"){
                $("#toggle-button").get(0).checked = true
                // $("#toggle-button").ch
            }

            if (data.showrmb === "1"){
                $('.isNoty').hide();
                $('.isTy').show();
                showrmb = 1;
            }

            $('#pwcode').html(data.pwcode);
        })
    }
    detail();
    $('#close').click(function () {
        nativeCommand('dancool://activity=fin')
    });
    $("#toggle-button").change(function () {
        if ($("#toggle-button").get(0).checked) {
            $('#pwcode').show()
        }else {
            $('#pwcode').hide();
        }
    });
    //同意协议
    $('.notyxdxy').click(function () {
        $(this).hide();
        $('.tyxdxy').show();
        show = 0;
    });
    //不同意协议
    $('.tyxdxy').click(function () {
        show = 1;
        $(this).hide();
        $('.notyxdxy').css('display','inline-block')
    });
    //同意挂卖到小程序
    $('.guaSmell .isNoty').click(function () {
        $(this).hide();
        $('.tyWXS').show();
        // showrmb = 1;
        $('.guaSmell').attr('data-showRmb','1');
    });
    //不同意挂卖到小程序
    $('.guaSmell .isTy').click(function () {
        // showrmb = 0;
        $(this).hide();
        $('.notyWXS').css('display','inline-block');
        $('.guaSmell').attr('data-showRmb','0');
    });
    //不同意挂卖app
    $('.guaApp .tyApp').click(function () {
        $(this).hide();
        // showApp = 0;
        $('.notyApp').css('display','inline-block');
        $('.guaApp').attr('data-showApp','0');
    });
    //同意挂卖app
    $('.guaApp .notyApp').click(function () {
        $(this).hide();
        $('.tyApp').show();
        $('.guaApp').attr('data-showApp','1');
        // showApp = 1;
    });
    //计算人民币
    $("#priceGold").bind("input propertychange", function() {
        let money = $(this).val();
        moneyrmb = Math.ceil(money/6)*100;
        let moneyRMB = Math.ceil(money / 6);
        $('#showrmbNum').html(moneyRMB);
        // console.log(moneyRMB);
    });

    //交易密码说明
    $('#passwprdInfo').click(function () {
        $('#conAlert2 .topInfo').html('打开密码交易将会生成随机密码，买家必须输入密码才可以购买商品。卖家可在咸蛋编辑页面刷新或关闭此项功能。注：该商品加密后将不在默认咸蛋页面显示，仅在筛选或搜索后显示。');
        $('#conAlert2').show();
    });
    //小程序说明
    $('#InfoDetail').click(function () {
        $('#conAlert2 .topInfo').html('同步以现金价格发布在蛋酱全有小程序内，在小程序内可以直接购买及提取收益。</br>' +
            '注1：现金交易后，蛋酱将收取交易总额的2%作为技术服务费，用户实际收到交易金额为扣除技术服务费后的成交金额。</br>' +
            '注2：商品售出金额将于成交24小时后才可以在小程序“咸蛋收益”进行提现。');
        $('#conAlert2').show();
    });

    $('#conBtn2').click(function () {
        $('#conAlert2').hide();
    });
    let ishidden,isshow = 2;
    $('#provateChat').click(function () {
        if (show === 1){
            laymsg('请同意咸蛋交易协议');
            return
        }
        let money2 = $('#priceGold').val();

        if ($("#toggle-button").get(0).checked) {
            ishidden = 1;
            // let password = $("#password").val();
            // if(password === ''){
            //     $.toast('请输入交易密码');
            //     return
            // }

        }else {
            ishidden = 0;
        }
        let showRmb = $('.guaSmell').attr('data-showRmb');
        let showApp = $('.guaApp').attr('data-showApp');
        if (showRmb === '0' && showApp === '0'){
            showrmb = '';
            laymsg('请选择挂卖方式');
            return;
        }else if (showRmb === '1' && showApp === '1'){
            showrmb = 1;
            isshow = 2
        }else if (showRmb === '1'){
            showrmb = 1;
            isshow = 1
        }else if (showApp === '1'){
            showrmb = 0;
            isshow = 0
        }
        console.log(isshow);
        let moneyRMB = moneyrmb / 100;
        let moneyFW = Math.ceil((moneyrmb / 10000 * 2 ));
        $('#moneyJin').html(money2+'金币');
        $('#moneyRMB').html('￥'+ moneyRMB);
        $('#moneyFW').html('￥'+ moneyFW);
        if (isshow === 0){
            $('.moneyRMB,.moneyFW').hide();
            $('.moneyJin').show();
        } else if(isshow === 1){
            $('.moneyJin').hide();
            $('.moneyRMB,.moneyFW').show();
        }else {
            $('.moneyJin,.moneyRMB,.moneyFW').show();
        }

        if(money2 === ''){
            laymsg('请输入挂卖价格');
            return;
        }

        if(money2 < 12){
            laymsg('挂卖价格不能小于12');
            return;
        }
        let price;
        let costPrice = cost*6*0.3;

        if (bukuan_status === '0'){
            price = dingjin_money2*0.5;
            if (money2 > dingjin_money2*2){
                laymsg('挂卖价格过高');
                return;
            }
        } else {
            price = exgprice*6*0.3;
        }


        if (price === 0){
            if (costPrice > money2 ){
                laymsg('挂卖价格风险较高，请重新定价或联系客服处理');
            }else {
                $('#conAlert').show();
            }
        }else {
            if (price > money2 ){
                laymsg('挂卖价格风险较高，请重新定价或联系客服处理');
            }else {
                $('#conAlert').show();
            }
        }
    });
    $('#conBtn').click(function () {
        upData()
    });
    $('#cancel').click(function () {
        $('#conAlert').hide();
    });

    function upData () {
        let intro = $('#intro').val();
        let money2 = $('#priceGold').val();
        let data = {
            'markid':markid,
            'kutoysid': xiandanid,
            'money2':money2,
            'intro':'',
            'showrmb':showrmb,
            'isshow':isshow,
            'moneyrmb':moneyrmb,
            'ispw':ishidden,

        };
        getData('post','/niu/apiv3/xiandanv3_doto.php?method=addupdate',data,function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                $('#conAlert').hide();
                laymsg(data.msg);
                setTimeout(function () {
                    nativeCommand("dancool://activity=reflashfin");
                    window.location.reload()
                },500)
            }else {
                $('#conAlert').hide();
                laymsg(data.msg)
            }
        })
    }
});
